package com.weipai.springboot.module.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Brand;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.service.BrandService;
import com.weipai.springboot.module.service.FileUploadService;
import com.weipai.springboot.module.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/07/27 17:51
 * 
 * 商品品牌管理类
 */
@RestController
public class BrandController {
    
    @Autowired
    BrandService brandService;
    @Autowired
    FileUploadService fileUploadService;

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页条数
     * @param draw 前台参数，照样返回chang
     * @param search 搜索，如果无搜索此值为paging
     * @param sort time
     * @param way desc，asc，noway
     */
    @GetMapping("/brand/list/{page}/{size}/{draw}/{search}/{sort}/{way}")
    public Map<String, Object> list(@PathVariable("page") Integer page,
                                           @PathVariable("size") Integer size,
                                           @PathVariable("draw") String draw,
                                           @PathVariable("search") String search,
                                           @PathVariable("sort") String sort,
                                           @PathVariable("way") String way){
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page, size);
        List<Brand> list = brandService.selectAllByBrandDel(0,search, sort, way);
        PageInfo<Brand> pageInfo = new PageInfo<>(list);
        if(list.size()>0){
            map.put("pageData", pageInfo.getList());
            map.put("recordsTotal", pageInfo.getTotal());
            map.put("draw", draw);
            map.put("pages", pageInfo.getPages());
            map.put("code",200);
        }else{
            map.put("code",401);
            map.put("msg","暂无数据");
        }
        return map;
    }

    /**
     * 添加品牌
     * @param request
     * @param brand
     * @return
     */
    @PostMapping("/brand/save")
    public Response save(HttpServletRequest request, Brand brand, MultipartFile img){
        Response response = new Response();
        brand.setCreateTime(DateUtils.timeFormat(System.currentTimeMillis()));
        if(img!=null){
            String result = fileUploadService.saveImgUpload(request, img);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if(result.equals("error")){
                response.setCode(RetCode.IMG_ERROR);
            }else{
                brand.setBrandLogo(result);
                response = brandService.save(brand);
            }
        }else{
            response = brandService.save(brand);
        }
        return response;
    }

    /**
     * 修改品牌
     * @param request
     * @param brand
     * @return
     */
    @PutMapping("/brand/update")
    public Response update(HttpServletRequest request, Brand brand, MultipartFile brandLogo){
        Response response = new Response();
        if(brandLogo!=null){
            String result = fileUploadService.saveImgUpload(request, brandLogo);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if(result.equals("error")){
                response.setCode(RetCode.IMG_ERROR);
            }else{
                brand.setBrandLogo(result);
                response = brandService.update(brand);
            }
        }else{
            response = brandService.update(brand);
        }
        return response;
    }

    /**
     * 查询品牌详情
     * @param brandId:品牌的id
     * @return
     */
    @GetMapping("/brand/getBrandById/{brandId}")
    public Response getBrandById(@PathVariable("brandId")  String brandId){
        return brandService.getBrandById(brandId);
    }

    /*
       查询 所有热销品牌
     */
    @GetMapping("/brand/getHotBrand")
    public Response getHotBrand(){
        return brandService.getHotBrand();
    }




    /**
     * 删除
     * @param brandId:品牌的id
     * @return
     */
    @DeleteMapping("/brand/deleteById/{brandId}")
    public Response deleteById(@PathVariable("brandId")  String brandId){
        /*
        删除品牌之前，首先判断这个品牌是否有商品使用，如果有的话不可以
         */
        return brandService.deleteById(brandId);
    }

    /**
     * 批量删除
     * @param id:品牌的id集合
     * @return
     */
    @DeleteMapping("/brand/batchDeleteById/{id}")
    public Response batchDeleteById(@PathVariable("id")  String[] id){
        return brandService.batchDeleteById(id);
    }

    /**
     * 更改品牌的状态
     * @param brandId:品牌的id
     * @return
     */
    @GetMapping("/brand/changeStatus/{brandId}/{status}")
    public Response changeStatus(@PathVariable("brandId")  String brandId,
                                            @PathVariable("status")  Integer status){
        /*
        更改品牌之前，首先判断这个品牌是否有商品使用，如果有的话不可以
         */
        return brandService.changeStatus(brandId,status);
    }

    /**
     * 通过分类id查询此分类下的品牌
     * @return
     */
    @GetMapping("/brand/getBrandByProCateId/{productCategoryId}")
    public Response getBrandByProCateId(@PathVariable("productCategoryId")String productCategoryId){
        return brandService.getBrandByProCateId(productCategoryId);
    }
}
